﻿Imports System.IO
Public Class frmThemHang

    Private Sub btnKiemTra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKiemTra.Click
        Dim th As TapHoa = New TapHoa()
        th.m_strTenHangHoa = txtTenSanPham.Text.ToString()
        Dim dt As DataTable = TapHoaDAO.TimHangHoaTheoTen(th)
        If (dt.Rows.Count = 0) Then
            lbThongBao.Text = "Sản phẩm chưa có trong kho !"

            txtGia.Enabled = True
            cbxDonVi.Enabled = True
            cbxLoaiHang.Enabled = True
            cbxNhaCungCap.Enabled = True
            cbxViTri.Enabled = True
            cbxXuatXu.Enabled = True
            btnChon.Enabled = True
        Else
            lbThongBao.Text = "Sản phẩm đã có trong kho !"

            Dim dt1 As DataTable = New DataTable()
            dt1 = TapHoaDAO.LayThongTinMotSanPhamTheoTen(th)
            txtGia.Text = dt.Rows(0)("hh_Gia").ToString()
            cbxDonVi.SelectedValue = Integer.Parse(dt.Rows(0)("hh_DonVi"))
            cbxLoaiHang.SelectedValue = Integer.Parse(dt.Rows(0)("hh_LoaiHang"))
            cbxNhaCungCap.SelectedValue = Integer.Parse(dt.Rows(0)("hh_NhaCungCap"))
            cbxViTri.SelectedValue = Integer.Parse(dt.Rows(0)("hh_ViTri"))
            cbxXuatXu.SelectedValue = Integer.Parse(dt.Rows(0)("hh_XuatXu"))
            picHinhAnh.ImageLocation = dt.Rows(0)("hh_HinhAnh").ToString()

            txtGia.Enabled = False
            cbxDonVi.Enabled = False
            cbxLoaiHang.Enabled = False
            cbxNhaCungCap.Enabled = False
            cbxViTri.Enabled = False
            cbxXuatXu.Enabled = False
            btnChon.Enabled = False
        End If
    End Sub

    Private Sub frmThemHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxLoaiHang.DataSource = TapHoaDAO.TatCaLoaiHang()
        cbxLoaiHang.DisplayMember = "lh_Loai"
        cbxLoaiHang.ValueMember = "lh_ID"

        cbxNhaCungCap.DataSource = TapHoaDAO.TatCaNhaCungCap()
        cbxNhaCungCap.DisplayMember = "ncc_Ten"
        cbxNhaCungCap.ValueMember = "ncc_ID"

        cbxXuatXu.DataSource = TapHoaDAO.TatCaXuatXu()
        cbxXuatXu.DisplayMember = "xx_NoiXuatXu"
        cbxXuatXu.ValueMember = "xx_ID"

        cbxDonVi.DataSource = TapHoaDAO.TatCaDonVi()
        cbxDonVi.DisplayMember = "dvt_Loai"
        cbxDonVi.ValueMember = "dvt_ID"

        cbxViTri.DataSource = TapHoaDAO.TatCaViTri()
        cbxViTri.DisplayMember = "vt_NoiVT"
        cbxViTri.ValueMember = "vt_ID"

        picHinhAnh.ImageLocation = "../../HinhAnh/noimage.jpg"
    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Dim dlg As OpenFileDialog = New OpenFileDialog()
        dlg.Filter = "Hình ảnh (*.png)|*.png|Tất cả (*.*)|*.*"
        If (dlg.ShowDialog() = DialogResult.OK) Then
            Dim filePath As String = dlg.FileName
            picHinhAnh.ImageLocation = Path.GetFullPath(filePath)
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim th As TapHoa = New TapHoa()
        th.m_strTenHangHoa = txtTenSanPham.Text.ToString()
        Dim dt As DataTable = TapHoaDAO.TimHangHoaTheoTen(th)
        If (dt.Rows.Count = 0) Then
            lbThongBao.Text = "Thêm thành công"
            ThemVaoDanhSach()
        End If
        If (dt.Rows.Count > 0 And txtGia.Enabled = True) Then
            lbThongBao.Text = "Sản phẩm này đã có trong kho chỉ có thể thêm số lượng !"
            Dim dt1 As DataTable = New DataTable()
            dt1 = TapHoaDAO.LayThongTinMotSanPhamTheoTen(th)
            txtGia.Text = dt.Rows(0)("hh_Gia").ToString()
            cbxDonVi.SelectedValue = Integer.Parse(dt.Rows(0)("hh_DonVi"))
            cbxLoaiHang.SelectedValue = Integer.Parse(dt.Rows(0)("hh_LoaiHang"))
            cbxNhaCungCap.SelectedValue = Integer.Parse(dt.Rows(0)("hh_NhaCungCap"))
            cbxViTri.SelectedValue = Integer.Parse(dt.Rows(0)("hh_ViTri"))
            cbxXuatXu.SelectedValue = Integer.Parse(dt.Rows(0)("hh_XuatXu"))
            picHinhAnh.ImageLocation = dt.Rows(0)("hh_HinhAnh").ToString()

            txtGia.Enabled = False
            cbxDonVi.Enabled = False
            cbxLoaiHang.Enabled = False
            cbxNhaCungCap.Enabled = False
            cbxViTri.Enabled = False
            cbxXuatXu.Enabled = False
            Return
        End If
        If (dt.Rows.Count > 0 And txtGia.Enabled = False) Then
            lbThongBao.Text = "Thêm thành công"
            ThemVaoDanhSach()
        End If
    End Sub

    Public Sub ThemVaoDanhSach()
        Dim DuongDan As String
        If (picHinhAnh.ImageLocation <> "../../HinhAnh/noimage.jpg") Then
            Dim directoryPath As String = "../../HinhAnh"
            Directory.CreateDirectory(directoryPath)
            Dim sourcefile As String = picHinhAnh.ImageLocation.ToString()
            Dim ChuyenDoi As String = directoryPath + "/" + Path.GetFileName(sourcefile)
            File.Copy(sourcefile, ChuyenDoi)
            DuongDan = ChuyenDoi
        Else
            DuongDan = "../../HinhAnh/noimage.jpg"
        End If

        Dim dgvDanhSach As Windows.Forms.DataGridView = Me.Tag
        dgvDanhSach.Rows.Add(txtTenSanPham.Text, txtGia.Text, cbxDonVi.SelectedValue, txtSoLuong.Text, cbxNhaCungCap.SelectedValue, cbxXuatXu.SelectedValue, cbxLoaiHang.SelectedValue, cbxViTri.SelectedValue, DuongDan)

        Dim TongTien As Integer = Integer.Parse(frmNhapHang.lbTongTien.Text.ToString())
        TongTien = TongTien + Integer.Parse(txtGia.Text * txtSoLuong.Text)
        Dim SoLuong As Integer = Integer.Parse(frmNhapHang.lbSoLuong.Text.ToString())
        SoLuong = SoLuong + Integer.Parse(txtSoLuong.Text)
        frmNhapHang.lbSoLuong.Text = SoLuong.ToString()
        frmNhapHang.lbTongTien.Text = TongTien.ToString()
    End Sub
End Class